import 'package:collection/collection.dart';
import 'package:flutter_soundmagus/widget/eq/eq.dart';
import 'package:json_annotation/json_annotation.dart';

part 'ch_group_model.g.dart';

@JsonSerializable()
class Ch {
  ///通道索引
  int index;

  ///是否联调
  bool isGroup;

  ///联调组索引
  int groupIndex;

  ///联调相对通道索引
  int relativeCh;

  Ch(
      {required this.index,
      required this.isGroup,
      required this.groupIndex,
      required this.relativeCh});

  Ch copyWith({int? index, bool? isGroup, int? relativeCh}) {
    this.index = index ?? this.index;
    this.isGroup = isGroup ?? this.isGroup;
    this.relativeCh = relativeCh ?? this.relativeCh;
    return this;
  }

  factory Ch.fromJson(Map<String, dynamic> json) => _$ChFromJson(json);

  Map<String, dynamic> toJson() => _$ChToJson(this);
}

@JsonSerializable(explicitToJson: true)
class ChConfig {
  ///选择通道
  int selectCh = 0;

  ///通道列表
  List<Ch> chs = List.generate(kChCount, (i) {
    /**
       * 0-8
       * 0,1=0
       * 2,3=1
       * 4,5=2
       * 6,7=3
       */
    final groupIndex = (i / 2).floor();
    /**
       * 0\2\4\6
       */
    final relativeCh = groupIndex * 2;
    var ch = Ch(
        index: i,
        isGroup: false,
        groupIndex: groupIndex,
        relativeCh: relativeCh);
    return ch;
  });

  ///设置组联调[groupIndex]组索引[relativeCH]相对通道
  void setGroup({required int groupIndex, required int relativeCH}) {
    var _chs = chs.where((e) => e.groupIndex == groupIndex);
    for (var e in _chs) {
      e.relativeCh = relativeCH;
      e.isGroup = true;
    }
  }

  ///取消组联调[groupIndex]组索引
  void cancelGroup({required int groupIndex}) {
    var _chs = chs.where((e) => e.groupIndex == groupIndex);
    for (var e in _chs) {
      e.isGroup = false;
    }
  }

  ///组是否联调[groupIndex]组索引
  bool isGroupSelected({required int groupIndex}) {
    return chs.firstWhereOrNull((e) => e.groupIndex == groupIndex)?.isGroup ==
        true;
  }

  ///取得通道组另外通道的索引
  int getRelativeCh({int? ch}) {
    var chi = ch ?? selectCh;
    var chObj = chs[chi];
    var relativeCH = chs
        .firstWhere((e) => e.groupIndex == chObj.groupIndex && e.index != chi)
        .index;
    return relativeCH;
  }

  ///通道是否选中[ch]通道索引
  bool isChSelected({required int ch}) {
    var currentCh = chs[ch], selectedCh = chs[selectCh];
    //1.判断当前通道
    if (selectCh == ch) {
      return true;
    }
    //2.判断是否有联调通道
    if (currentCh.isGroup) {
      var groupCh = chs
          .where((e) =>
              e.isGroup &&
              e.index != ch &&
              e.groupIndex == selectedCh.groupIndex &&
              e.groupIndex == currentCh.groupIndex)
          .firstOrNull;
      if (groupCh != null) {
        return true;
      }
    }
    return false;
  }

  ///通道是否处于联调状态
  bool isChGrouped({required int ch}) {
    var currentCh = chs[ch];

    //2.判断是否有联调通道
    if (currentCh.isGroup) {
      var groupCh = chs
          .where((e) =>
              e.isGroup &&
              e.index != ch &&
              e.groupIndex == currentCh.groupIndex)
          .firstOrNull;
      if (groupCh != null) {
        return true;
      }
    }
    return false;
  }

  ///获取通道选择状态
  List<bool> selecteds() {
    var ch = selectCh;
    var chSelects = List.generate(chs.length, (index) => false);
    final srcCH = chs[ch];
    for (var e in chs) {
      if (srcCH.index == e.index) {
        chSelects[e.index] = true;
      } else if (srcCH.isGroup &&
          e.groupIndex == srcCH.groupIndex &&
          e.index != srcCH.index) {
        //如果是联调
        chSelects[e.index] = true;
      } else {
        chSelects[e.index] = false;
      }
    }
    return chSelects;
  }

  ChConfig();

  factory ChConfig.fromJson(Map<String, dynamic> json) =>
      _$ChConfigFromJson(json);

  Map<String, dynamic> toJson() => _$ChConfigToJson(this);
}
